package com.kamistoat.meimeimall.meimeimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.meimeimallproduct.entity.AttrEntity;
import com.kamistoat.meimeimall.meimeimallproduct.service.AttrService;
import com.kamistoat.meimeimall.meimeimallproduct.service.CategoryService;
import com.kamistoat.meimeimall.meimeimallproduct.vo.AttrGroupRelationVo;
import com.kamistoat.meimeimall.meimeimallproduct.vo.AttrGroupWithAttrVo;
import com.kamistoat.meimeimall.meimeimallproduct.entity.AttrGroupEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeimall.meimeimallproduct.service.AttrGroupService;
import com.kamistoat.meimeimall.common.utils.R;


/**
 * 属性分组
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 15:16:42
 */
@Api(tags = "属性分组数据库操作")
@RestController
@RequestMapping("attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;

    /**
     * 查询当前分组下的所有基础属性/销售属性
     *
     * @param attrgroupId 属性分组id
     * @return R
     */
    @ApiOperation("查询指定属性分组下的所有属性")
    @GetMapping("/attr_under_attrgroup/{attrgroupId}")
    @DubboAndHTTPExceptionCatch
    public R attrAttrGroupRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        return R.ok();
    }

    /**
     * 删除当前属性分组下的关联关系，只删关系，不是删除基础属性
     *
     * @param relationVos 列表，元素都是AttrGroupRelationVo
     * @return R
     */
    @ApiOperation("删除属性分组和属性之间的绑定关系")
    @DeleteMapping("/attr_under_attrgroup_delete")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R attrAttrGroupRelationDelete(@RequestBody List<AttrGroupRelationVo> relationVos) {
        return R.ok();
    }


    /**
     * 为属性分组增加与基础属性的关联
     *
     * @param relationVos
     * @return R
     */
    @ApiOperation("为属性分组增加与基础属性的关联")
    @PutMapping("/attr_under_attrgroup_add")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R attrAddRelation(@RequestBody List<AttrGroupRelationVo> relationVos) {
        return R.ok();
    }

    /**
     * 返回三级分类下的属性分组列表并嵌套属性列表
     *
     * @param categoryId 三级分类ID
     * @return R
     */
    @ApiOperation("返回指定商品分类Id下的属性分组列表并嵌套属性列表")
    @GetMapping("/nested_attrgroup_under_category/{categoryId}")
    @DubboAndHTTPExceptionCatch
    public R attrGroupWithAttr(@PathVariable("categoryId") Long categoryId) {
        List<AttrGroupWithAttrVo> attrGroupWithAttrVoList = attrGroupService.getAttrGroupWithAttr(categoryId);
        return R.ok().put("data", attrGroupWithAttrVoList);
    }

    /**
     * 列表
     */
    @ApiOperation("从数据库获取列表")
    @GetMapping("/list")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 只查询对应菜单的属性列表
     */
    @ApiOperation("查询指定商品菜单Id下的属性分组")
    @GetMapping("/attrgroup_under_category/{categoryId}")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attrgroup:list")
    public R listSingle(@PathVariable("categoryId") long categoryId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params, categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("向数据查询指定POJO")
    @GetMapping("/info/{attrGroupId}")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 首先获取出该属性分组所属的菜单Id
        Long catelogId = attrGroup.getCatelogId();
        // 在CategoryService中定义一个寻找全部父菜单Id的方法，直接接收上面获取到的catelogId，返回一个long[]
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        // 返回前端的时候带着这个变量
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @ApiOperation("向数据库保存POJO")
    @PutMapping("/attrgroup_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("向数据库更新POJO")
    @PostMapping("/attrgroup_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("从数据库删除POJO")
    @DeleteMapping("/attrgroup_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        // TODO 还得删除一系列属性的关联关系
        return R.ok();
    }

}
